<template>
	<view class="content">
		<view class="itembox" v-if="userInfo.userId">
			<view class="item" @click="sheetShow = true">
				<view class="avatar_title">头像</view>
				<view class="">
					<image :src="userInfo.avatar" class="avatar_img" mode="aspectFill"></image>
					<image src="http://cdn.yic3.cn/lawyer/mp-xhs/mine/rightIcon.png" mode="widthFix"></image>
				</view>
			</view>
			<view class="item">
				<view class="avatar_title">姓名</view>
				<view class="">
					<input @blur="userInfoChange()" type="text" v-model="userInfo.name" />
					<image src="http://cdn.yic3.cn/lawyer/mp-xhs/mine/rightIcon.png" mode="widthFix"></image>
				</view>
			</view>
			<view class="item">
				<view class="avatar_title">用户ID</view>
				<view class="">
					{{userInfo.userId}}
				</view>
			</view>
			<view class="item" @click="apply">
				<view class="name_title">性别</view>
				<view class="input_box">
					<p v-if="userInfo.sex">{{userInfo.sex==2?'女':'男'}}</p>
					<p v-else>选择性别</p>
					<image src="http://cdn.yic3.cn/lawyer/mp-xhs/mine/rightIcon.png" mode="widthFix"></image>
				</view>
			</view>
			<view class="item" @click="applys">
				<view class="name_title">年龄</view>
				<view class="input_box">
					<p>{{userInfo.age|| '选择年龄'}}</p>
					<image src="http://cdn.yic3.cn/lawyer/mp-xhs/mine/rightIcon.png" mode="widthFix"></image>
				</view>
			</view>
		</view>
		<u-action-sheet :actions="sheetList" @select="selectHanlder" :show="sheetShow" closeOnClickOverlay
			cancelText="取消" @close="sheetShow = false"></u-action-sheet>
		<u-picker :show="show" title="性别" @confirm="confirm" @cancel="cancel" :columns="columns"></u-picker>
		<u-picker :show="ageshow" title="年龄" @confirm="confirms" @cancel="cancel" :columns="column"></u-picker>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions
	} from 'vuex'
	export default {
		data() {
			return {
				sheetList: [{
						name: '拍摄'
					},
					{
						name: '从相册选择'
					}
				],
				sheetShow: false,
				show: false,
				ageshow: false,
				column: [],
				columns: [
					['女', '男']
				],
			};
		},
		computed: {
			...mapState({
				userInfo: state => state.userInfo
			})
		},
		onLoad() {
			var arr = Array.from({
				length: 100
			}, (v, k) => k + 18);
			this.column[0] = arr
		},
		methods: {
			// 选择上传图片方式
			...mapActions({
				getUserInfo: 'getUserInfo'
			}),
			async selectHanlder(e) {
				let that = this
				if (e.name == '拍摄') {
					// #ifdef APP
					plus.android.requestPermissions(
						['android.permission.CAMERA'],
						function(res) {
							if (res.granted.length) {
								uni.setStorageSync('cameraAuth', 1)
								that.cameraAuth = 1
							}
							if (res.deniedAlways.length) {
								that.cameraAuth = -1
								uni.setStorageSync('cameraAuth', -1)
							}
						})
					// #endif

					this.uploadImg(2)
				} else {
					// #ifdef APP
					plus.android.requestPermissions(
						['android.permission.WRITE_EXTERNAL_STORAGE'],
						function(res) {
							if (res.granted.length) {
								uni.setStorageSync('albumAuth', 1)
								that.albumAuth = 1
							}
							if (res.deniedAlways.length) {
								that.albumAuth = -1
								uni.setStorageSync('albumAuth', -1)
							}
						})
					// #endif
					this.uploadImg(1)
				}
			},
			async uploadImg(index) {
				let that = this
				await uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: [index == 1 ? 'album' : 'camera'], //调取相机
					success: async function(RES) {
						console.log(RES);
						const tempFilePaths = RES.tempFilePaths;
						that.imageUrl = await that.$upload(tempFilePaths[0])
						that.userInfo.avatar = that.imageUrl
						console.log(that.imageUrl);
						let data = {
							avatar: that.imageUrl,
						}
						that.$api.userInfoChange(data).then(res => {
							if (res.data.code == 0) {
								uni.showToast({
									icon: 'success',
									title: '头像修改成功',
									duration: 2000
								});
							}
							that.getUserInfo()
						})
					},
					fail: (err) => {
						if (err.errMsg == 'chooseImage:fail No Permission') {
							uni.showToast({
								title: index == 2 ? "未授权相机权限" : '未授权相册权限',
								icon: 'none'
							})
						}

					}
				});
			},
			userInfoChange() {
				let data = {
					name: this.userInfo.name,
					// config: "{server.course.id:2}"
				}
				this.$api.userInfoChange(data).then(res => {
					if (res.data.code == 0) {
						uni.showToast({
							icon: 'success',
							title: '用户名修改成功',
							duration: 2000
						});
					}
				})
			},
			confirm(e) {
				this.show = false
				this.sex = e.value[0]
				let data = {
					sex: e.value[0] == '男' ? '1' : '2',
					// config: "{server.course.id:2}"
				}
				this.$api.userInfoChange(data).then(res => {
					if (res.data.code == 0) {
						this.getUserInfo()
						uni.showToast({
							icon: 'success',
							title: '性别修改成功',
							duration: 2000
						});
					}
				})

			},
			confirms(e) {
				this.ageshow = false
				this.age = e.value[0]
				let data = {
					age: e.value[0],
					// config: "{server.course.id:2}"
				}
				this.$api.userInfoChange(data).then(res => {
					if (res.data.code == 0) {
						this.getUserInfo()
						uni.showToast({
							icon: 'success',
							title: '年龄修改成功',
							duration: 2000
						});
					}
				})
			},
			cancel() {
				this.show = false
				this.ageshow = false
			},
			apply() {
				this.show = !this.show
			},
			applys() {
				this.ageshow = !this.show
			}
		},
	}
</script>

<style lang="scss" scoped>
	@import 'userinfo.scss';
</style>